package com.example.maple.ccsclient.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.maple.ccsclient.R;

public class InputActivity extends Activity {

	String mTitle;
	TextView mTextView = null;
	EditText mEditText = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input);

		findView();
	}

	private void findView() {

		mTitle = getIntent().getStringExtra("title");
		mTextView = (TextView) findViewById(R.id.titleText);
		mTextView.setText(mTitle);
		mEditText = (EditText) findViewById(R.id.inputContent);
		mEditText.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
					InputMethodManager imm = (InputMethodManager) v.getContext()
							.getSystemService(Context.INPUT_METHOD_SERVICE);
					if (imm.isActive()) {

						imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),
								0);

					}
					sendMsg();

					return true;
				}
				return false;
			}
		});
		findViewById(R.id.commit).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				sendMsg();
				Log.d("点击了提交按钮", "点击了提交按钮");
			}
		});


		findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});


	}

	private void sendMsg() {

		String info = mEditText.getText().toString();
		if (null == info || info.isEmpty() || info.equals("")) {
			Toast.makeText(InputActivity.this, "请输入", Toast.LENGTH_SHORT).show();
			return;
		}
		switch (mTitle) {
			case "文字输入": {
				SearchActivity.startAction(InputActivity.this, info);
				finish();
				break;
			}

			case "文章查询": {

				ReportActivity.startAction(InputActivity.this, info);
				finish();
				break;
			}
		}
	}


	public static void startAction(Context context, String par) {
		Intent intent = new Intent(context, InputActivity.class);
		intent.putExtra("title", par);
		context.startActivity(intent);


	}
}
